import ctypes
from cProfile import label
from tkinter import Label, ttk
import tkinter
import re
from operator import xor
from turtle import width
import xlsxwriter
import os
import xlrd


def read():
    print('''
***************
**************  注意  **************
请检查本程序所在目录下有没有文件: a.xls
请将学院下发的实训安排表另存为: a.xls
在xls文件中:
::------只保留每个老师的课表-------
::其它工作表删除
::检查下有没有隐藏的工作表，也删除
''')


read()
input('按任意键继续……')
# 打开一个Excel文件，创建一个工作簿对象


def open_xlsx(file):
    try:
        fh = xlrd.open_workbook(file)
    except:
        read()
        input('输入任意键，结束本程序后，重新启动。')
        exit()
    return fh


# 获取sheet表的个数
def get_sheet_num(fh):
    x = len(fh.sheets())
    return x


# 读取文件内容并返回行内容
def get_file_content(fh, shnum):
    table = fh.sheets()[shnum]
    num = table.nrows
    print('\r\b', " "*50, '\r\b', table, '完成')
    week = 0
    for row in range(num):
        rdata = table.row_values(row)
        rdata = [str(x).strip() for x in rdata]
        rdata = rdata[1:8]
        try:
            if (rdata[0]):
                week = eval(rdata[0])
            rdata[0] = week
            datavalue.append(rdata)
        except:
            del (rdata)
    return datavalue
# 根据周次，节次，星期 查询已排课的教室


def look():
    try:
        wk = int(comWk.get())
        jc = comJc.get()
        wd = int(comWd.get())
        print('\n\n第%s周 星期%s 第%s节 安排的实训教室有： ' % (wk,  wd, jc))
        out = [
            x[wd + 1] for x in datavalue if x[0] == wk and x[1] == jc and x[wd + 1]
        ]
        out.sort(key=lambda k: int(''.join(re.findall(r'\d', k))))
        t = 0
        for x in out:
            if(x.split('-')[0] != t):
                t = x.split('-')[0]
                print()
            print(x, end='\t')
        print('\n')
    except:
        print('\n::请选择正确的 周次、星期、节次！')


# 存储所有读取的结果
datavalue = []
# 设置数据文件的路径
fl = "a.xls"
fh = open_xlsx(fl)
sheet_num = get_sheet_num(fh)
print('正在加载数据……')
for shnum in range(sheet_num):
    rvalue = get_file_content(fh, shnum)
print('数据读取完成!')

# 提取数据中的周次、节次
weeks = list(set(int(x[0]) for x in rvalue if x[0]))
weeks.sort()
jc = list(set(x[1] for x in rvalue if x[1]))
jc.sort(key=lambda k: int(''.join(re.findall(r'\d', k))))
# 初始化星期列表
weekd = [1, 2, 3, 4, 5]

# 定义查询窗口
window = tkinter.Tk()
window.title('请选择查询条件-By: isZhuPeng@qq.com')
# w = 460
# h = 30
l = window.winfo_screenwidth()//2
t = window.winfo_screenheight()//2
window.geometry(f'+{l}+{t}')
ctypes.windll.shcore.SetProcessDpiAwareness(1)
ScaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0)
# 设置缩放因子
window.tk.call('tk', 'scaling', ScaleFactor/75)

comWk = ttk.Combobox(window, height=20, width=10)
comWd = ttk.Combobox(window, height=20, width=10)
comJc = ttk.Combobox(window, height=20, width=10)
btnLook = ttk.Button(window, command=look)
comWk.grid(column=2, row=1)
comWd.grid(column=4, row=1)
comJc.grid(column=6, row=1)
btnLook.grid(column=7, row=1)
Label(window, text='周次').grid(column=1, row=1)
Label(window, text='星期').grid(column=3, row=1)
Label(window, text='节次').grid(column=5, row=1)

comWk['value'] = ['请选择周次'] + weeks
comWk.current(0)
comJc['value'] = ['请选择节次']+jc
comJc.current(0)
comWd['value'] = ['请选择星期']+weekd
comWd.current(0)
btnLook['text'] = '查询'


window.mainloop()
# print()
